<?php

namespace app\admin\controller;

use app\common\lib\Param;
use app\common\lib\Show;
use app\common\lib\Str;
use app\Request;
use think\facade\Cache;
use think\response\Json;

class RepresentMessage extends AdminBase
{

    public function index(): Json
    {
        $page=$this->request->get('page');
        $limit=$this->request->get('limit');
        $name=$this->request->get('name');

        $bis = new \app\common\business\RepresentMessage();

        $map = [];
        !empty($name) && $map[] = ['name','like','%'.$name.'%'];

        try {
            $list = $bis->getListByWhereWithOrder($map,$page,$limit);
            $count = $bis->getCountByWhere($map);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }

        $result = [
            'count'=>$count,
            'list'=>$list
        ];
        return Show::success($result);
    }

    /**
     * 根据id删除
     * @return Json
     */
    public function deleteById(): Json
    {
        $id=$this->request->get('id');
        try {
            $result=(new \app\common\business\RepresentMessage())->deleteById($id);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($result) {
            return Show::success([], "删除成功");
        }
        return Show::error([], '删除失败');
    }
}
